package cn.cau.control;


import cn.cau.entity.*;
import cn.cau.service.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import cn.cau.dao.BasicEnumDao;
import cn.cau.dao.CategoryDao;
import org.omg.CORBA.Request;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Iterator;
import java.util.List;
@Controller
public class RuleSearchController {
    @Autowired
    private CodeRuleService codeRuleService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private BasicEnumService basicEnumService;
    @Autowired
    private NonEnumService nonEnumService;
    @Autowired
    private CodeNumberService codeSearchService;



    @RequestMapping(value="/inputKey2",method=RequestMethod.POST)
    public ModelAndView inputKey2(
            HttpServletRequest request,
            HttpSession session,
            @RequestParam(value = "rule_string")String rule_string)
    {
        ModelAndView modelAndView = new ModelAndView();
        String errorString="";
        List<CodeRule> c1=null;
        int rulecategory=Integer.valueOf(request.getParameter("rulecategorys"));
        if(rulecategory==0)
        {
            if(rule_string!="")
            {
                c1 = codeRuleService.findCodeRuleByName(rule_string);
                modelAndView.addObject("rule_string_result",rule_string);
            }
        }
        else
        {
            if(rule_string!="")
            {
                List<CodeRule> c2 = codeRuleService.findCodeRuleByName(rule_string);
                if(c2==null||c2.size()==0)
                {
                    errorString = "查询结果为空，请重新输入查询标识";
                    modelAndView.addObject("error", errorString);
                    modelAndView.setViewName("/codeSearch/ruleSearch");
                    return modelAndView;
                }
                int c2CategoryId=c2.get(0).getRuleCategoryId();
                if(rulecategory==c2CategoryId)
                {
                    c1 = codeRuleService.findCodeRuleByName(rule_string);
                }
                else
                {
                    errorString = "规则库不含所输入的标识，请重新输入！";
                    modelAndView.addObject("error", errorString);
                    modelAndView.setViewName("/codeSearch/ruleSearch");
                    return modelAndView;
                }
            }
            else
            {
                c1 = codeRuleService.findCodeRuleByCategoryId(rulecategory);
                String cate2 = codeSearchService.getRuleCategoryName(rulecategory);
                modelAndView.addObject("rulecategorys_result", cate2);
            }
        }

        if(c1==null||c1.size()==0)
        {
            errorString = "查询结果为空，请重新输入查询标识";
            modelAndView.addObject("error", errorString);
            modelAndView.setViewName("/codeSearch/ruleSearch");
            return modelAndView;
        }
        else
        {
            modelAndView.addObject("coderuleresult",c1);
            modelAndView.addObject("error", errorString);
            modelAndView.setViewName("/codeSearch/ruleSearch");
            return modelAndView;
        }

    }

    @RequestMapping(value="/ruleSearchApply",method=RequestMethod.GET)
    public ModelAndView RuleSearchApply(@RequestParam(value = "id")int cc){
        ModelAndView modelAndView = new ModelAndView();
        CodeRule rule=codeRuleService.findCodeRuleById(cc);
        String rulecategoryname=codeSearchService.getRuleCategoryName(rule.getRuleCategoryId());
        String categoryname=codeSearchService.getCategoryName(rule.getCategoryId());
        String rulename=rule.getCodeRuleName();
        int codestate=rule.getCodeState();
        int codecode=rule.getCodeCodeCount();
        int serial=rule.getSerialNumber();
        String codestatee=null;
        List<BasicEnumSum> enumsum=basicEnumService.findBasicEnumSumByCodeRuleId(rule.getCodeRuleId());
        List<NonEnum> nonenumsum=nonEnumService.findNonEnumByCodeId(rule.getCodeRuleId());
        String codeintro="包含";
        for (int i = 0; i <enumsum.size() ; i++) {
            codeintro+=enumsum.get(i).getDetails();
            if(i!=enumsum.size()-1)
            {
                codeintro+="、";
            }
        }
        for (int i = 0; i <nonenumsum.size() ; i++) {
            codeintro+=nonenumsum.get(i).getDetails();
            if(i!=nonenumsum.size()-1)
            {
                codeintro+="、";
            }
        }
        codeintro+="码节点";
        if(codestate==0)
        {
            codestatee="未设计完成";
        }
        else if(codestate==1)
        {
            codestatee="已提交审核";
        }
        else if(codestate==2)
        {
            codestatee="正在工作（通过审核）";
        }
        else if(codestate==3)
        {
            codestatee="未通过审核";
        }
        else
        {
            codestatee="已废除";
        }
        int codeid=rule.getCodeRuleId();
        modelAndView.addObject("number",rulename);
        modelAndView.addObject("rule",rule);
        modelAndView.addObject("codestatee",codestatee);
        modelAndView.addObject("coderuleid",codeid);
        modelAndView.addObject("codecode",codecode);
        modelAndView.addObject("serial",serial);
        modelAndView.addObject("codeintro",codeintro);
        modelAndView.addObject("rulecategoryname",rulecategoryname);
        modelAndView.addObject("categoryname",categoryname);
        modelAndView.setViewName("/codeSearch/ruleSearchResult");

        return modelAndView;
    }


}
